package com.uairobot.bill.jfinal;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.server.undertow.UndertowConfig;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import com.litong.utils.ip.IPUtil;
import com.uairobot.bill.freeswitch.controller.FsController;
import com.uairobot.bill.freeswitch.controller.FsTaskController;
import com.uairobot.bill.freeswitch.controller.PhoneNumberController;
import com.uairobot.bill.freeswitch.controller.StartTaskController;
import com.uairobot.bill.freeswitch.model._MappingKit;
import com.uairobot.bill.jfinal.generator.JFinalGenerator;
import com.uairobot.bill.jfinal.utils.PropKitUtil;

public class Application extends JFinalConfig {
  private static String configFileName = PropKitUtil.configFileName;

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    UndertowServer server = UndertowServer.create(Application.class, configFileName);
    server.start();
    UndertowConfig undertowConfig = server.getUndertowConfig();
    int port = undertowConfig.getPort();
    String contextPath = undertowConfig.getContextPath();
    long end = System.currentTimeMillis();
    System.out.println(IPUtil.getThisUrl(port, contextPath));
    System.out.println("启动完成,共使用了" + (end - start) + "ms");
  }

  public void configConstant(Constants me) {
    me.setInjectDependency(true);
    me.setInjectSuperClass(true);
  }

  public void configRoute(Routes me) {
    me.setMappingSuperClass(true);
    me.add("/fs", FsController.class);
    me.add("/fsTask",FsTaskController.class);
    me.add("/phoneNumber",PhoneNumberController.class);
    me.add("startTask",StartTaskController.class);
  }

  @Override
  public void configEngine(Engine me) {
  }

  @Override
  public void configPlugin(Plugins me) {
    // 配置 druid 数据库连接池插件
    DruidPlugin druidPlugin = JFinalGenerator.createDruidPlugin();
    me.add(druidPlugin);  
    // 配置ActiveRecord插件
    ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
    // 所有映射在 MappingKit 中自动化搞定
    _MappingKit.mapping(arp);
    me.add(arp);
  }

  @Override
  public void configInterceptor(Interceptors me) {
  }

  @Override
  public void configHandler(Handlers me) {
  }
}